home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Newbie Questions
- Date: Sun, 24 Mar 96 00:47:36 GMT
- Organization: none
- Message-ID: <827628456snz@genesis.demon.co.uk>
- References: <4irpc1$b8u@GRAPEVINE.LCS.MIT.EDU> <4isgta$7cr@newsbf02.news.aol.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4isgta$7cr@newsbf02.news.aol.com>
- mvaccaro1@aol.com "MVaccaro1" writes:
-
- >>Question #2
- >>
- >>Why does the scope of the external declaration not extend beyond >the
- >>inner block?
-
- The extern keyword has no effect on scope (i.e. where in the source file
- a particular declaration is visible). It can affect linkage (i.e. whether
- two declarations for the same identifier refer to the same object/function),
- duration (i.e. it determines or at least indicates when the object the
- declaration refers to is created and destroyed). It can also prevent an
- object declaration becoming a definition.
-
- The terminology can be confusing here since an 'external declaration'
- simply means one that appears outside of a function i.e. at file scope and
- has nothing to do with the extern keyword at all.
-
- >>{
- >> {
- >> extern E;
- >> E = 0;
- >> }
- >> E = 1;
- >>}
- >
- >Your declare of the external variable E is only known in the block it is
- >declared in. Had you wrote the code this way:
- >
- >extern int E;
- >
- >func( ... )
- >{
- > {
- > E=0;
- > }
- >E=1;
- >}
- >
- >E would be known to all references in the file.
- >
- >Where as
- >
- >func( ... )
- > {
- > extern int E;
- > {
- > E = 0;
- > }
- > E = 1;
- > }
- >
- >E would only be known within function.
-
- The interesting case is:
-
- int E; /* a */
-
- func( ... )
- {
- int E; /* b */
-
- {
- extern int E; /* c */
- E=0;
- }
-
- E=1;
- }
-
- The declarations a and c (and hence the E=0) refer to the same object because
- of linkage. b (and the E=1) refer to an entirely separate local variable
- in the function.
-
- >>Question #3
- >>
- >>Is there any difference between these two declarations?
- >>
- >> char *s1 = "hello";
-
- The creates a static array of char initialised to
- { 'h', 'e', 'l', 'l', 'o', '\0' }. s1 itself is defined as a char * pointer
- and initialised to point to the first character of the array. s1 is a
- normal variable that you can modify, it is illegal to modify the character
- array.
-
- >> char s2[] = "hello";
-
-
- Here s2 is defined to be an array of char initialised to
- { 'h', 'e', 'l', 'l', 'o', '\0' }. You can modify the elements of the
- array but there is no pointer here to modify as there is with s1.
-
- >try this little test and tell me what happens:
- >
- >main( )
- > {
- > char *s1 = "hello";
- > char s2[ ] = "hello";
- >
- > printf( "Sizeof s1 %d\n Sizeof s2 %d\n", sizeof s1, sizeof s2 );
-
- Anything can happen since sizeof doesn't return an int value, it returns
- a value with some implementation specific unsigned type (size_t). The
- correct way to write this is:
-
- printf( "Sizeof s1 %lu\n Sizeof s2 %lu\n", (unsigned long) sizeof s1,
- (unsigned long) sizeof s2 );
-
-
- > return 0;
- > }
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-